<?php
namespace App\Services;
use App\Models\IntegralRule;
use App\Models\SinInLog;
class SinInLogService{
    /**
     * 用户每日签到
     * @param Request $request  传入用户标识
     * @return array
     */
    public function userSinIn($request){
        $sinInLog = new SinInLog();
        $now = now()->toDateString();
        $last_sign_info = $request->user()->sinInLogs()->orderby("id","desc")->first();
        if ($last_sign_info){
            $last_sign_date = $last_sign_info->created_at->toDateString();
            //如果是同一天签到
            if ($last_sign_date==$now){
                $state = [
                    "msg"=>"今日已签到",
                    "code"=>422,
                ];
                return  $state;
            }
        }
        $integral = IntegralRule::getSignInIntegral();
        $sinInLog->user()->associate($request->user());
        $sinInLog->save();
            $state = [
                "msg"=>"签到成功",
                "integral"=>$integral,
                "code"=>201,
            ];
        return $state;
    }
}
